package servlet;

import java.io.*;
import java.sql.*;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import util.*;
import bll.*;
import model.*;
import dal.*;

/**
 * Servlet implementation class CompanyServlet
 */
@WebServlet(urlPatterns = { "/TeamAllocation", "/teamAllocation", "/TeamAllocationServlet", "/teamAllocationServlet" }, initParams = { @WebInitParam(name = "mode", value = "") })
public class TeamAllocationServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * Default constructor.
	 */
	public TeamAllocationServlet() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html; charset=UTF-8");

		String mode = request.getParameter("mode");

		if (mode.equalsIgnoreCase("new")) {

			if (RightUtil.<TeamAllocationModel> checkInsertRight()) {

				// TODO : MORE CODE HERE

				getServletContext().getRequestDispatcher("/teamAllocationNew.jsp").forward(request, response);
			} else {
				// TODO no right
			}
		} else if (mode.equalsIgnoreCase("edit")) {

			TeamAllocationModel query = new TeamAllocationModel();
			String id = request.getParameter("id");
			query.setId(Integer.parseInt(id));

			GenericDal<TeamAllocationModel, TeamAllocationModelExt> dal = new GenericDal<TeamAllocationModel, TeamAllocationModelExt>(new TeamAllocationModel());

			TeamAllocationModel obj = null;
			try {
				obj = dal.select(query);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			if (obj != null) {

				// TODO : MORE CODE HERE

				request.setAttribute("teamAllocation", obj);
				getServletContext().getRequestDispatcher("/teamAllocationEdit.jsp").forward(request, response);

			} else {
				// TODO : data not found
			}
		} else if (mode.equalsIgnoreCase("view")) {

			TeamAllocationModel query = new TeamAllocationModel();
			String id = request.getParameter("id");
			query.setId(Integer.parseInt(id));

			GenericDal<TeamAllocationModel, TeamAllocationModelExt> dal = new GenericDal<TeamAllocationModel, TeamAllocationModelExt>(new TeamAllocationModel());

			TeamAllocationModel obj = null;
			try {
				obj = dal.select(query);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			if (obj != null) {
				// TODO : LOAD MASTER DATA HERE
				// It's specific code that cannot be generated

				request.setAttribute("teamAllocation", obj);
				getServletContext().getRequestDispatcher("/teamAllocationView.jsp").forward(request, response);

			} else {
				// TODO : data not found
			}
		} else if (mode.equalsIgnoreCase("delete")) {

			TeamAllocationModel query = new TeamAllocationModel();
			String id = MiscUtil.getStringUrlParam(request, "id");
			int idProject = MiscUtil.getIntUrlParam(request, "idProject");

			query.setId(Integer.parseInt(id));

			GenericDal<TeamAllocationModel, TeamAllocationModelExt> dal = new GenericDal<TeamAllocationModel, TeamAllocationModelExt>(new TeamAllocationModel());

			int res = Integer.MIN_VALUE;
			try {
				res = dal.delete(query);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			if (res == 0) {
				// TODO success
				if (idProject != Integer.MIN_VALUE) {
					response.sendRedirect("teamAllocationNew.jsp?idProject=" + idProject);
				} else {
					response.sendRedirect("teamAllocationList.jsp");
				}
			} else {
				// TODO fail
			}
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html; charset=UTF-8");

		String mode = request.getParameter("mode");

		if (mode.equalsIgnoreCase("insert")) {

			TeamAllocationModel obj = new TeamAllocationModel();

			String id_project = request.getParameter("id_project");
			String sentFrom = request.getParameter("sentFrom");
			if (sentFrom == null)
			{
				sentFrom = "";//sentFrom.trim();
			}

			if (id_project != null && !id_project.isEmpty()) {
				obj.setIdProject(Integer.parseInt(id_project));
			}

			String id_phase = request.getParameter("id_phase");
			if (id_phase != null && !id_phase.isEmpty()) {
				obj.setIdPhase(Integer.parseInt(id_phase));
			}

			String id_employee = request.getParameter("id_employee");
			if (id_employee != null && !id_employee.isEmpty()) {
				obj.setIdEmployee(Integer.parseInt(id_employee));
			}

			String id_project_role = request.getParameter("id_project_role");
			if (id_project_role != null && !id_project_role.isEmpty()) {
				obj.setIdProjectRole(Integer.parseInt(id_project_role));
			}

			String man_day = request.getParameter("man_day");
			if (man_day != null && !man_day.isEmpty()) {
				obj.setManDay(Integer.parseInt(man_day));
			}

			String description = request.getParameter("description");
			obj.setDescription(description);

			String remark = request.getParameter("remark");
			obj.setRemark(remark);

			String status = request.getParameter("status");
			if (status != null && !status.isEmpty()) {
				obj.setStatus(Integer.parseInt(status));
			}

			obj.setCreator(SessionUtil.getCurrentUserID());

			if (CommonBll.checkInputs(obj)) {
				GenericDal<TeamAllocationModel, TeamAllocationModelExt> dal = new GenericDal<TeamAllocationModel, TeamAllocationModelExt>(new TeamAllocationModel());

				int res = 0;
				try {
					res = dal.insert(obj);
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (ClassNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				if (res > 0) {
					// TODO : success, redirect to somewhere
					response.sendRedirect("teamAllocationNew.jsp?idProject=" + obj.getIdProject() + (sentFrom != "" ? ("&sentFrom=" + id_project) : ""));
				} else {
					// TODO : processing error here
				}
			} else {

				// invalid input

				request.setAttribute("teamAllocation", obj);
				getServletContext().getRequestDispatcher("/teamAllocationInput.jsp").forward(request, response);

			}
		} else if (mode.equalsIgnoreCase("update")) {

			TeamAllocationModel query = new TeamAllocationModel();
			String id = request.getParameter("id");
			query.setId(Integer.parseInt(id));

			GenericDal<TeamAllocationModel, TeamAllocationModelExt> dal = new GenericDal<TeamAllocationModel, TeamAllocationModelExt>(new TeamAllocationModel());

			TeamAllocationModel obj = null;
			try {
				obj = dal.select(query);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			if (obj != null) {

				String id_project = request.getParameter("id_project");
				if (id_project != null && !id_project.isEmpty()) {
					obj.setIdProject(Integer.parseInt(id_project));
				}

				String id_phase = request.getParameter("id_phase");
				if (id_phase != null && !id_phase.isEmpty()) {
					obj.setIdPhase(Integer.parseInt(id_phase));
				}

				String id_employee = request.getParameter("id_employee");
				if (id_employee != null && !id_employee.isEmpty()) {
					obj.setIdEmployee(Integer.parseInt(id_employee));
				}

				String id_project_role = request.getParameter("id_project_role");
				if (id_project_role != null && !id_project_role.isEmpty()) {
					obj.setIdProjectRole(Integer.parseInt(id_project_role));
				}

				String man_day = request.getParameter("man_day");
				if (man_day != null && !man_day.isEmpty()) {
					obj.setManDay(Integer.parseInt(man_day));
				}

				String description = request.getParameter("description");
				obj.setDescription(description);

				String remark = request.getParameter("remark");
				obj.setRemark(remark);

				String status = request.getParameter("status");
				if (status != null && !status.isEmpty()) {
					obj.setStatus(Integer.parseInt(status));
				}

				if (CommonBll.checkInputs(obj)) {

					int res = Integer.MIN_VALUE;
					try {
						res = dal.update(obj);
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (ClassNotFoundException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

					if (res == 0) {
						// TODO success
						getServletContext().getRequestDispatcher("/teamAllocationNew.jsp").forward(request, response);
					} else {
						// TODO error
					}
				} else {
					request.setAttribute("teamAllocation", obj);
					getServletContext().getRequestDispatcher("/teamAllocationEdit.jsp").forward(request, response);
				}
			} else {
				// TODO data not found
			}
		} else if (mode.equalsIgnoreCase("delete")) {

			TeamAllocationModel query = new TeamAllocationModel();
			String id = request.getParameter("id");
			query.setId(Integer.parseInt(id));

			GenericDal<TeamAllocationModel, TeamAllocationModelExt> dal = new GenericDal<TeamAllocationModel, TeamAllocationModelExt>(new TeamAllocationModel());

			int res = Integer.MIN_VALUE;
			try {
				res = dal.delete(query);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			if (res == 0) {
				// TODO success
				getServletContext().getRequestDispatcher("/teamAllocationList.jsp").forward(request, response);
			} else {
				// TODO fail
			}
		} else if (mode.equalsIgnoreCase("quickInsert")) {
			TeamAllocationModel obj = new TeamAllocationModel();

			// TODO set name here

			GenericDal<TeamAllocationModel, TeamAllocationModelExt> dal = new GenericDal<TeamAllocationModel, TeamAllocationModelExt>(new TeamAllocationModel());

			int res = 0;
			try {
				res = dal.insert(obj);
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			// TODO : return value

		}
	}

}